#include "alarmmodel.h"

AlarmModel::AlarmModel(QObject *parent)
    : QAbstractListModel(parent)
    , m_nextId(1)
{
}

int AlarmModel::rowCount(const QModelIndex &parent) const
{
    if (parent.isValid())
        return 0;
    return m_alarms.count();
}

QVariant AlarmModel::data(const QModelIndex &index, int role) const
{
    if (!index.isValid() || index.row() >= m_alarms.count())
        return QVariant();

    const Alarm &alarm = m_alarms[index.row()];

    switch (role) {
    case IdRole:
        return alarm.id;
    case StationIdRole:
        return alarm.stationId;
    case MessageRole:
        return alarm.message;
    case SeverityRole:
        return alarm.severity;
    case TimestampRole:
        return alarm.timestamp.toString("yyyy-MM-dd HH:mm:ss");
    case AcknowledgedRole:
        return alarm.acknowledged;
    case StationNameRole:
        return alarm.stationName;
    case LatitudeRole:
        return alarm.latitude;
    case LongitudeRole:
        return alarm.longitude;
    case SignalStrengthRole:
        return alarm.signalStrength;
    case ConnectedUsersRole:
        return alarm.connectedUsers;
    case StatusRole:
        return alarm.status;
    default:
        return QVariant();
    }
}

QHash<int, QByteArray> AlarmModel::roleNames() const
{
    QHash<int, QByteArray> roles;
    roles[IdRole] = "alarmId";
    roles[StationIdRole] = "stationId";
    roles[MessageRole] = "message";
    roles[SeverityRole] = "severity";
    roles[TimestampRole] = "timestamp";
    roles[AcknowledgedRole] = "acknowledged";
    roles[StationNameRole] = "stationName";
    roles[LatitudeRole] = "latitude";
    roles[LongitudeRole] = "longitude";
    roles[SignalStrengthRole] = "signalStrength";
    roles[ConnectedUsersRole] = "connectedUsers";
    roles[StatusRole] = "status";
    return roles;
}

void AlarmModel::addAlarm(const QString &stationId, const QString &message, const QString &severity)
{
    beginInsertRows(QModelIndex(), 0, 0);
    
    Alarm alarm;
    alarm.id = QString::number(m_nextId++);
    alarm.stationId = stationId;
    alarm.message = message;
    alarm.severity = severity;
    alarm.timestamp = QDateTime::currentDateTime();
    alarm.acknowledged = false;
    
    m_alarms.prepend(alarm);
    
    endInsertRows();

    emit newAlarmAdded(stationId, message, severity);
}

void AlarmModel::acknowledgeAlarm(int index)
{
    if (index < 0 || index >= m_alarms.count())
        return;

    m_alarms[index].acknowledged = true;
    QModelIndex modelIndex = this->index(index);
    emit dataChanged(modelIndex, modelIndex);
}

void AlarmModel::clearAll()
{
    if (m_alarms.isEmpty())
        return;

    beginRemoveRows(QModelIndex(), 0, m_alarms.count() - 1);
    m_alarms.clear();
    endRemoveRows();
}

int AlarmModel::unacknowledgedCount() const
{
    int count = 0;
    for (const auto &alarm : m_alarms) {
        if (!alarm.acknowledged)
            count++;
    }
    return count;
}

void AlarmModel::addAlarmWithDetails(const QString &stationId, const QString &message, const QString &severity,
                                     const QString &stationName, double latitude, double longitude,
                                     double signalStrength, int connectedUsers, const QString &status)
{
    beginInsertRows(QModelIndex(), 0, 0);
    
    Alarm alarm;
    alarm.id = QString::number(m_nextId++);
    alarm.stationId = stationId;
    alarm.message = message;
    alarm.severity = severity;
    alarm.timestamp = QDateTime::currentDateTime();
    alarm.acknowledged = false;
    
    // 保存基站详细信息快照
    alarm.stationName = stationName;
    alarm.latitude = latitude;
    alarm.longitude = longitude;
    alarm.signalStrength = signalStrength;
    alarm.connectedUsers = connectedUsers;
    alarm.status = status;
    
    m_alarms.prepend(alarm);
    
    endInsertRows();

    emit newAlarmAdded(stationId, message, severity);
}

QVariantMap AlarmModel::getAlarmInfo(int index) const
{
    QVariantMap info;
    
    if (index < 0 || index >= m_alarms.count())
        return info;
    
    const Alarm &alarm = m_alarms[index];
    
    info["alarmId"] = alarm.id;
    info["stationId"] = alarm.stationId;
    info["stationName"] = alarm.stationName;
    info["message"] = alarm.message;
    info["severity"] = alarm.severity;
    info["timestamp"] = alarm.timestamp.toString("yyyy-MM-dd HH:mm:ss");
    info["acknowledged"] = alarm.acknowledged;
    info["latitude"] = alarm.latitude;
    info["longitude"] = alarm.longitude;
    info["signalStrength"] = alarm.signalStrength;
    info["connectedUsers"] = alarm.connectedUsers;
    info["status"] = alarm.status;
    
    return info;
}

int AlarmModel::getTotalCount() const
{
    return m_alarms.count();
}

int AlarmModel::getCountByStation(const QString &stationId) const
{
    int count = 0;
    for (const auto &alarm : m_alarms) {
        if (alarm.stationId == stationId) {
            count++;
        }
    }
    return count;
}

